package com.lijy.awt;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Lijingyu on 2017/10/20.
 */
public class Separation extends Applet {

    TextField
            t = new TextField(20),
            mod = new TextField(20);
    BusinessLogic bl = new BusinessLogic(2);
    Button
            calc1 = new Button("Calculation 1"),
            calc2 = new Button("Calculation 2");

    public void init() {
        add(t);
        calc1.addActionListener(new Calc1L());
        calc2.addActionListener(new Calc2L());
        add(calc1); add(calc2);
        mod.addTextListener(new ModL());
        add(new Label("Modifier:"));
        add(mod);
    }

    private class Calc1L implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            t.setText(Integer.toString(bl.calculation1(getValue(t))));
        }
    }

    static int getValue(TextField tf) {
        try {
            return Integer.parseInt(tf.getText());
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static void main(String[] args) {
        Separation applet = new Separation();
        Frame aFrame = new Frame("Separation");
        aFrame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
        aFrame.add(applet, BorderLayout.CENTER);
        aFrame.setSize(200, 200);
        applet.init();
        applet.start();
        aFrame.setVisible(true);
    }

    private class Calc2L implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            t.setText(Integer.toString(bl.calculation2(getValue(t))));
        }
    }

    private class ModL implements TextListener {
        public void textValueChanged(TextEvent e) {
            bl.setModifier(getValue(mod));
        }
    }

}

class BusinessLogic {
    private int modifier;

    BusinessLogic(int mod) {
        modifier = mod;
    }

    // Some business operations:
    public int calculation1(int arg) {
        return arg * modifier;
    }

    public int calculation2(int arg) {
        return arg + modifier;
    }

    public void setModifier(int mod) {
        modifier = mod;
    }

    public int getModifier() {
        return modifier;
    }

}